//
//  BVIndexViewController.m
//  XiangYu
//
//  Created by YYKJ on 2017/5/31.
//  Copyright © 2017年 YYKJ. All rights reserved.
//

#import "BVIndexViewController.h"
#import "BVIndexCell1.h"
#import "BVIndexDetailsViewController.h"
#import "BVSearchViewController.h"
#import "BVIndexTool.h"
#import "BVRefreshPch.h"

#define BUY_INDEX_COLLECTIONVIEW_CELL1 @"BVIndexCell1"

@interface BVIndexViewController ()<UICollectionViewDataSource,UICollectionViewDelegate,UICollectionViewDelegateFlowLayout>

@property (nonatomic ,strong) UICollectionView             *collectionView;
@property (nonatomic ,strong) UICollectionViewFlowLayout   *layout;
@property (nonatomic ,strong) BVIndexTool *indexTool;
@property (nonatomic ,strong) NSMutableArray *dataArr;

@end

@implementation BVIndexViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self.view setBackgroundColor:[UIColor BVBackgroundLightGrayColor]];
    [self.navigationItem setTitle:@"推荐"];
    
    [self setNav];
    [self layoutUI];
    [self setupRefresh];
}

-(void)setNav{
    
    UIButton *rightBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [rightBtn setFrame:CGRectMake(0, 10, 26, 28)];
    [rightBtn setBackgroundImage:[UIImage imageNamed:@"xiaoxi"] forState:UIControlStateNormal];
    UIBarButtonItem* rightBarBtn = [[UIBarButtonItem alloc] initWithCustomView:rightBtn];
    [self.navigationItem setRightBarButtonItem:rightBarBtn];
    
    UIButton *searchBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [searchBtn setImage:[UIImage imageNamed:@"seach"] forState:UIControlStateNormal];
    [searchBtn setTitle:@"快速搜索" forState:UIControlStateNormal];
    [searchBtn.titleLabel setFont:[UIFont systemFontOfSize:14]];
    [searchBtn setBackgroundColor:[UIColor colorWithRed:153/255.0 green:153/255.0 blue:153/255.0 alpha:0.5]];
    [searchBtn addTarget:self action:@selector(searchBtnClick:) forControlEvents:UIControlEventTouchUpInside];
    [searchBtn setCenterX:ScreenW/2];
    [searchBtn setWidth:200];
    [searchBtn setHeight:30];
    [searchBtn setCenterY:20];
    [searchBtn setImageEdgeInsets:UIEdgeInsetsMake(5, 10, 5, 160)];
    [searchBtn.layer setMasksToBounds:YES];
    [searchBtn.layer setCornerRadius:15.0];
    [self.navigationItem setTitleView:searchBtn];
    
}

-(void)setupRefresh{
    
    self.collectionView.mj_header = [BVRefreshHeader headerWithRefreshingBlock:^{
        [self fetchIndexData];
    }];
    self.collectionView.mj_footer = [BVRefreshFooter footerWithRefreshingBlock:^{
        [self.collectionView.mj_footer endRefreshingWithNoMoreData];
        
    }];
}

-(void)layoutUI{
    
    UIButton *photoBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    [self.view addSubview:photoBtn];
    
    self.layout=[[UICollectionViewFlowLayout alloc]init];
    [self.layout setItemSize:CGSizeMake (ScreenW/2-3, ScreenW/2-5)];
    [self.layout setSectionInset:UIEdgeInsetsMake(2, 2, 2, 2)];
    [self.layout setMinimumLineSpacing:2.0];
    [self.layout setMinimumInteritemSpacing:2.0];
    
    self.collectionView = [[UICollectionView alloc] initWithFrame:CGRectMake(0, 0, ScreenW, ScreenH-64-49) collectionViewLayout:self.layout];
    [self.view addSubview:self.collectionView];
    [self.collectionView setBackgroundColor:[UIColor clearColor]];
    [self.collectionView setDelegate:self];
    [self.collectionView setDataSource:self];
    
    [self.collectionView registerClass:[BVIndexCell1 class] forCellWithReuseIdentifier: BUY_INDEX_COLLECTIONVIEW_CELL1];
}

#pragma mark -- UICollectionViewDataSource ,UICollectionViewDelegate
-(NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    return self.dataArr.count;
}

-(UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    
    BVIndexCell1 * cell = (BVIndexCell1 *)[collectionView dequeueReusableCellWithReuseIdentifier:BUY_INDEX_COLLECTIONVIEW_CELL1 forIndexPath:indexPath];
    [cell setModel:self.dataArr[indexPath.row]];
    return cell;
}

-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:( NSIndexPath *)indexPath{

    BVIndexDetailsViewController *indexDetails = [[BVIndexDetailsViewController alloc]init];
    ROOTNAVPUSH(indexDetails, YES);
}

-(void)fetchIndexData{
    
    [self.indexTool fetchSystemRecommendedPage:@"0" Success:^(NSMutableArray *arr) {
        [self.collectionView.mj_header endRefreshing];
        [self.dataArr removeAllObjects];
        [self.dataArr addObjectsFromArray:arr];
        [self.collectionView reloadSections:[NSIndexSet indexSetWithIndex:0]];
    } codeFailure:^(NSString *str) {
        [self.collectionView.mj_header endRefreshing];
        [self showMessage:str];
    } failure:^(NSError *error) {
        [self.collectionView.mj_header endRefreshing];
        [self showError:@"网络错误"];
    }];
    
}

-(void)searchBtnClick:(UIButton *)btn{
    
    BVSearchViewController *search = [[BVSearchViewController alloc]init];
    ROOTNAVPUSH(search ,YES);
    
}

-(BVIndexTool *)indexTool{
    if (!_indexTool) {
        _indexTool = [[BVIndexTool alloc]init];
    }
    return _indexTool;
}

-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
